<template>
  <div class="app-container calendar-list-container">
    <div class="w-body">
      <el-form ref="entity" label-width="80px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="采购订单" prop="orderNo">
              <el-input v-model="orderNo" :readonly="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户" prop="customerName">
              <el-input v-model="customerName" :readonly="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="w-table">
        <buttons :showQuery="true" :showBack="true" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="订单号" :permissions="permissions" @add="doAdd"></buttons>
        <grid gridTitle="入库订单" :columns="columns" :enableEdit="false" :url="url" :loading="true" ref="mainGrid" height="auto" diffHeight="125" :pagination="true"
          :permissions="permissions" :queryEntity="queryEntity" :multi="false" @change="rowCilck" @edit="doEdit" @view="doView" @pass="doPass" @cancel="doCancel"
          @delete="doDelete">
        </grid>
      </div>
    </div>
  </div>
</template>
<script>
import request from '@/router/axios'
import { Message, MessageBox } from 'element-ui'
import url from '@/const/apiURL'
import GridDialog from '@/components/GridDialog'
import grid from '@/components/Grid'
import { mapGetters } from 'vuex'
import { queryField } from '@/const/query'
import buttons from '@/components/Buttons'
import { PurchaseInOrderStatusFormatter, YesOrNoFormatter } from '@/const/dict'
import { bgColor } from '@/const/bgColor'

export default {
  components: { GridDialog, grid, buttons },
  data () {
    return {
      orderNo: '',
      customerName: '',
      url: url.PURCHASE_IN_LIST_URL,
      disabled: true,
      queryEntity: {},
      entity: {},
      columns: [
        {
          label: '操作', width: 160, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '编辑', clickName: 'edit', auth: 'purchase_in_edit', valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '查看', clickName: 'view', auth: 'purchase_in_view' },
            { name: '通过', clickName: 'pass', auth: 'purchase_in_pass', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '取消', clickName: 'cancel', auth: 'purchase_in_cancel', confirm: true, valueProp: "orderStatus", showValue: ["UN_IN", "FINISH"] },
            { name: '删除', clickName: 'delete', auth: 'purchase_in_delete', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] }
          ]
        },
        { label: '采购单号', width: 180, prop: 'orderNo', align: 'center' },
        { label: '入库单号', width: 180, prop: 'inCode', align: 'center' },
        { label: '外部单号', width: 180, prop: 'outBusinessCode', align: 'center' },
        { label: '仓库名称', width: 100, prop: 'warehouseName', align: 'center' },
        { label: '入库数量', width: 100, prop: 'quantity', align: 'center' },
        { label: '入库卷数', width: 80, prop: 'total', align: 'center' },
        { label: '总价', width: 100, prop: 'totalAmount', align: 'center' },
        { label: '入库时间', width: 100, prop: 'outSendTime', align: 'center' },
        { label: '订单状态', width: 80, prop: 'orderStatus', align: 'center', formatter: PurchaseInOrderStatusFormatter, template: 'bgColor', bgColor: bgColor().PurchaseInOrderStatus },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '入库', type: 'primary', icon: 'edit', click: 'add', auth: 'purchase_in_add', ctr: false },
        // { name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'purchase_in_edit', ctr: true },
        //{ name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'purchase_in_view', ctr: true },
        //{ name: '通过', type: 'primary', icon: 'edit', click: 'pass', auth: 'purchase_in_pass', ctr: true, confirm: true },
        //{ name: '取消', type: 'primary', icon: 'edit', click: 'cancel', auth: 'purchase_in_cancel', ctr: true, confirm: true },
        //{ name: '删除', type: 'primary', icon: 'edit', click: 'delete', auth: 'purchase_in_delete', ctr: true, confirm: true }
      ]
    }
  },
  created () {
    this.orderNo = this.$route.query.orderNo
    this.customerName = this.$route.query.customerName
    this.queryEntity.orderNo = this.$route.query.orderNo
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    doAdd () {
      if (this.orderNo) {
        this.$router.push({ path: '/prod/purchase/in/add', query: { orderNo: this.orderNo, customerName: this.customerName } })
      } else {
        MessageBox.alert("请选择订单");
      }
    },
    doEdit (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({ path: '/prod/purchase/in/edit', query: { orderNo: this.orderNo, customerName: this.customerName, inCode: row.inCode } })
      }
    },
    doView (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({ path: '/prod/purchase/in/view', query: { orderNo: this.orderNo, customerName: this.customerName, inCode: row.inCode } })
      }
    },
    openOrder () {
      this.$refs['orderGridDialog'].show()
    },
    selectOrderData (row) {
      this.orderNo = row.orderNo
      this.initData()
    },
    doCancel (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.cancelOp(row.inCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    cancelOp (inCode) {
      return request({
        url: url.PURCHASE_IN_CANCEL_URL,
        method: 'post',
        data: { key: 'key' },
        params: { inCode: inCode }
      })
    },
    doDelete (row) {
      //const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.deleteOp(row.inCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    deleteOp (inCode) {
      return request({
        url: url.PURCHASE_IN_DELETE_URL,
        method: 'post',
        data: { key: 'key' },
        params: { inCode: inCode }
      })
    },
    doPass (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.passOp(row.inCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    passOp (inCode) {
      return request({
        url: url.PURCHASE_IN_PASS_URL,
        method: 'post',
        data: { key: 'key' },
        params: { inCode: inCode }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.w-body {
  padding-top: 8px;
}
.w-table {
  border: 1px solid #ebeef5;
}
.w-table-title {
  border: 1px solid #ebeef5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
  line-height: 23px;
  display: inline-block;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: relative;
  vertical-align: middle;
  width: 100%;
  font-size: 14px;
  color: gray;
}
</style>
